package top.fyukina.fymil;

import org.apache.commons.io.FilenameUtils;
import org.dom4j.DocumentException;
import top.fyukina.fymil.elements.Program;
import top.fyukina.fymil.elements.ProgramPack;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Fymil {
    public static void main(String[] args) throws DocumentException, FileNotFoundException {
        //TODO 命令行参数执行程序
        String path = System.getProperty("user.dir") + File.separator + "main.xml";
        ProgramPack pack = new ProgramPack();
        if (args == null || args.length == 0) {
            Program program = Loader.loadProgram(new FileInputStream(path));
            pack.addProgram(program, true);
        } else {
            File f = new File(args[0]);
            if (f.exists()) {
                path = args[0];
                String ext = FilenameUtils.getExtension(f.getName());
                if ("xml".equalsIgnoreCase(ext) || "fym".equalsIgnoreCase(ext)) {
                    Program program = Loader.loadProgram(new FileInputStream(path));
                    pack.addProgram(program, true);
                } else if ("fmpack".equalsIgnoreCase(ext)) {
                    //TODO 程序包加载
                }
            }
        }
        //TODO 全局异常处理器实现
        try {
            pack.getMain().run(pack, new Registry());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}